package cn.derry.servlet;

import cn.derry.domain.Account;
import cn.derry.domain.Message;
import cn.derry.server.ChatServer;
import cn.derry.util.ResponseUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author MangoDai
 * @decription 消息请求
 * @date 2017/12/20
 **/
public class MessageServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("MessageServlet 初始化成功" );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Account account = (Account) req.getSession().getAttribute(LoginServlet.SESSION_USER);
        String to = req.getParameter("to");
        System.out.println("获取 " + account.getUsername() + " 与 " + to + " 的消息");
        List<Message> chatRecord = ChatServer.getChatRecord(account.getId(), to);
        ResponseUtil.renderJson(resp,JSON.toJSONString(chatRecord));
    }
}
